/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.playlist;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.Resources;
import org.shake.lastfm.model.Track;

public class TrackRenderer extends JXPanel implements ListCellRenderer
{
    private static final long serialVersionUID = -5798259139262640948L;

    private final DefaultListCellRenderer titleRenderer;
    private final DefaultListCellRenderer timeRenderer;
    
    public TrackRenderer()
    {
        this.titleRenderer = new DefaultListCellRenderer();
        this.timeRenderer = new DefaultListCellRenderer();
        setLayout(new BorderLayout(7, 0));

        add(this.timeRenderer, BorderLayout.WEST);
        add(this.titleRenderer, BorderLayout.CENTER);
    }
    
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        this.titleRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (value instanceof Track)
        {
            Track track = (Track)value;
            this.timeRenderer.getListCellRendererComponent(list, 
                    track.getFormattedDuration(), index, false, false);
            
            switch (track.getStatus())
            {
                case SCHEDULED:
                    this.titleRenderer.setIcon(Resources.getIcon("icon.scheduled.16x16"));
                    break;
                case IN_PROGRESS:
                    this.titleRenderer.setIcon(Resources.getIcon("icon.saving.16x16"));
                    break;
                case ON_DISK:
                    this.titleRenderer.setIcon(Resources.getIcon("icon.done.16x16"));
                    break;
                case FAILED:
                    this.titleRenderer.setIcon(Resources.getIcon("icon.failed.16x16"));
                    break;
                default:
                    this.titleRenderer.setIcon(null);
            }
        }
        
        return this;
    }
}
